/*
 * --------------------------------------------------------------------
 * @name OpenCVProcesor.java
 * @author Juan Camilo Ibarra
 *
 * This work is licensed under a Creative
 * Commons Attribution 3.0 License. This means you can to copy, 
 * distribute, transmit and adapt the work, under the following condition:
 * You must attribute the work in the manner specified by the author or licensor.
 *
 * Juan Camilo Ibarra	-	juanibarral@gmail.com
 * Oct 18, 2011
 * --------------------------------------------------------------------
 */
package kernel;

//TODO: Uncomment for using wiht JavaCv
//import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;
//import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
//import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
//import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GAUSSIAN;
//import static com.googlecode.javacv.cpp.opencv_imgproc.cvSmooth;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Observable;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import util.IniFilesReader;

/**
 * @author JuanK
 *
 */
public class OpenCVProcesor extends Observable{

	private static OpenCVProcesor instance;
	private String classifierName;

	private String currentImage;

	//TODO: Erase, only for test
	private int values[] = new int[9];

	private OpenCVProcesor()
	{
		try {
			setup();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void setup()throws Exception
	{
		Map<String, String> params = IniFilesReader.readIniFile("./ext/javacv.config");
		classifierName = params.get("opencv.data.path");
		classifierName = classifierName.concat(params.get("opencv.classifier"));
	}

	public static OpenCVProcesor getInstance()
	{
		if(instance == null)
		{
			instance = new OpenCVProcesor();
		}
		return instance;
	}

//	public String smooth(String inputFilename, String outputFilename, int cv_size) { 
//		System.out.println();
//		IplImage image = cvLoadImage(inputFilename);
//		if (image != null) {
//			cvSmooth(image, image, CV_GAUSSIAN, cv_size);
//			//TODO: find out how to do it without using secondary memory for updating the gui
//			cvSaveImage(outputFilename, image);
//			cvReleaseImage(image);
//		}
//		return inputFilename + "\n" + outputFilename + "\n" + cv_size;
//	}

	public void FindFace(String inputFileName)
	{

	}

//	public void smooth(String inputFilename, BufferedImage outputBufferedImage,int cv_size)
//	{
//		//TODO: there is an issue creating an IplImage with a BufferedImage. Use this code when fixed
//		// changing the first parameter to BufferedImage inputBufferedImage
//		//		IplImage image = IplImage.createFrom(inputBufferedImage);
//		IplImage image = cvLoadImage(inputFilename);
//		if(image != null)
//		{
//			cvSmooth(image, image, CV_GAUSSIAN, cv_size);
//			image.copyTo(outputBufferedImage);
//			//			outputBufferedImage = image.getBufferedImage();
//			cvReleaseImage(image);
//		}
//	}

	public void treatment_01(int value, String filePath)
	{
		values[0] = value;
		testChangingImage(filePath);
	}
	public void treatment_02(int value, String filePath)
	{
		values[1] = value;
		testChangingImage(filePath);
	}
	public void treatment_03(int value, String filePath)
	{
		values[2] = value;
		testChangingImage(filePath);
	}
	public void treatment_04(int value, String filePath)
	{
		values[3] = value;
		testChangingImage(filePath);
	}
	public void treatment_05(int value, String filePath)
	{
		values[4] = value;
		testChangingImage(filePath);
	}
	public void treatment_06(int value, String filePath)
	{
		values[5] = value;
		testChangingImage(filePath);
	}
	public void treatment_07(int value, String filePath)
	{
		values[6] = value;
		testChangingImage(filePath);
	}
	public void treatment_08(int value, String filePath)
	{
		values[7] = value;
		testChangingImage(filePath);
	}
	public void treatment_09(int value, String filePath)
	{
		values[8] = value;
		testChangingImage(filePath);
	}


	private void testChangingImage(String imageFilePath) 
	{

		ImageIcon imageIcon = new ImageIcon(imageFilePath);

		int iconWidth = imageIcon.getIconWidth();
		int iconHeight = imageIcon.getIconHeight();
		Image image = imageIcon.getImage();
		BufferedImage buffer = new BufferedImage(iconWidth, iconHeight, BufferedImage.TYPE_INT_ARGB);
		Graphics graphics = buffer.createGraphics();

		graphics.drawImage(image, 0, 0, iconWidth, iconHeight, null);
		if(values!= null)
		{
			Font font = new Font("Serif", Font.PLAIN, 15);
			graphics.setFont(font);
			graphics.setColor(Color.black);
			graphics.setFont(font);
			for(int i = 0; i < values.length; i++)
			{

				String numberSt = String.valueOf(values[i]);
				numberSt = "Treatment 0" + (i + 1) + ": "+ numberSt;


				graphics.drawChars(numberSt.toCharArray(), 0, numberSt.length(), 10, 15 * (i + 1));
			}
		}
		imageIcon = new ImageIcon(buffer);
		imageIcon.getImage().flush();

		
		graphics.dispose();
		setChanged();
		notifyObservers(buffer);
	}

	public void saveFile(BufferedImage bufferedImage, String imageFilePath)
	{
		File outputFile = new File(imageFilePath + ".png");
		try {
			ImageIO.write(bufferedImage, "png", outputFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getCurrentImage() {
		return currentImage;
	}


}
